package b1.controls.tab
{
	import b1.conifg.*;
	import b1.controls.basic.*;
	import b1.scenes.*;
	import b1.utils.*;
	
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.filters.ColorMatrixFilter;
	import flash.text.*;
	import flash.ui.Mouse;

	public class TabController extends Sprite
	{
		public var tabs:Array;
		public var tabTitles:Array;
		public var currentTabIndex:int;
		public var type:int;
		private var currentTabTileFilter:ColorMatrixFilter = new ColorMatrixFilter(
					[1,0,0,0,20,
				     0,1,0,0,20,
				     0,0,1,0,20,
				     0,0,0,1,0 ]);
		private var mouseOverTabTileFilter:ColorMatrixFilter = new ColorMatrixFilter(
					[1,0,0,0,15,
				     0,1,0,0,15,
				     0,0,1,0,15,
				     0,0,0,1,0 ]);
				     
		//type: 0,探案、作案、商店的对话框； 1，回家界面中的包裹； 2，作案界面中的包裹；
		public function TabController(BackgroundImage:Class, CloseButtonImage:Class = null, type:int = 0)
		{
			super();
			var backgroundImage:Sprite = new BackgroundImage();
			if (type == 0)
			{
				backgroundImage.width = 750;
				backgroundImage.height = 426;
			}
			else
			{
				backgroundImage.width = 830;
				backgroundImage.height = 270;
			}
			addChild(backgroundImage);
			tabs = new Array();
			tabTitles = new Array();
			currentTabIndex = 0;
			this.type = type;
			if (CloseButtonImage != null)
			{
				//关闭按钮
				var closeButton:BasicButton = new BasicButton(CloseButtonImage);
				closeButton.x = this.width - closeButton.width - 38;
				closeButton.y = 10;
				addChild(closeButton);
				closeButton.addEventListener(MouseEvent.CLICK, onCloseButtonClick);
			}
		}
		
		public function addTab(TabTitleBackground:Class, tab:Tab):void
		{
			//标签
			if (type == 0)
			{
				var tabTitle:TabTitle = new TabTitle(TabTitleBackground, tab.tabName, tabs.length);
				tabTitle.x = 100 + tabs.length * (tabTitle.width + 5);
				tabTitle.y = -5;
//				tabTitle.filters = [currentTabTileFilter];
				tabTitle.addEventListener(MouseEvent.CLICK, onMouseClick);
				tabTitle.addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
				tabTitle.addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
				tabTitles.push(tabTitle);
				addChild(tabTitle);
			}
			else
			{
				var tabIcon:TabIcon; 
				if (type == 1)
				{
					tabIcon = new TabIcon(TabTitleBackground, tab.tabName, tabs.length, true);
					tabIcon.y = 15;
					tabIcon.x = 60 + tabs.length * (tabIcon.width + 5);
				}
				else  if (type == 2)
				{
					tabIcon = new TabIcon(TabTitleBackground, tab.tabName, tabs.length, true);
					tabIcon.y = 10;
					tabIcon.x = 60 + tabs.length * (tabIcon.width + 5);
				}
				else
				{
					tabIcon = new TabIcon(TabTitleBackground, tab.tabName, tabs.length);
					tabIcon.x = 40 + tabs.length * (tabIcon.width + 5);
				}
				tabIcon.addEventListener(MouseEvent.CLICK, onMouseClick);
				tabTitles.push(tabIcon);
				addChild(tabIcon);
			}
			//标签页
			if (type == 0)
			{
				tab.x = 5;
				tab.y = 80;
			}
			else if (type == 1)
			{
				tab.x = 5;
				tab.y = 50;
			}
			else if (type == 2)
			{
				tab.x = 0;
				tab.y = 37;
			}
			if (tabs.length != currentTabIndex)
			{
				tab.disable();
			}
			else
			{
				tab.enable();
				if (type == 0)
					tabTitle.filters = [currentTabTileFilter];
			}
			tabs.push(tab);
			addChild(tab);
		}
		
		private function onMouseClick(event:MouseEvent):void
		{
			if (SceneContainer.getInstance().mouseDisableFlag && !SceneContainer.getInstance().checkOnlyMousePosition(event.currentTarget))
				return;
			if (event.currentTarget.index != currentTabIndex)
			{
				tabs[currentTabIndex].disable();
				tabs[event.currentTarget.index].enable();
				tabTitles[currentTabIndex].filters = null;
				event.currentTarget.filters = [currentTabTileFilter];
				currentTabIndex = event.currentTarget.index;
			}
		}
		
		private function onMouseOver(event:MouseEvent):void
		{
			var tabTitle:TabTitle = event.currentTarget as TabTitle;
			if (tabTitle.index != currentTabIndex)
			{
				tabTitle.filters = [mouseOverTabTileFilter];
			}
			Mouse.cursor = "button";
		}
		
		private function onMouseOut(event:MouseEvent):void
		{
			var tabTitle:TabTitle = event.currentTarget as TabTitle;
			if (tabTitle.index != currentTabIndex)
			{
				tabTitle.filters = null;
			}
			Mouse.cursor = "auto";
		}

		private function onCloseButtonClick(event:MouseEvent):void
		{
			if (SceneContainer.getInstance().mouseDisableFlag && !SceneContainer.getInstance().checkOnlyMousePosition(event.currentTarget))
				return;
				
			if (this.parent != null)
			{
				this.parent.removeChild(this);
			}
		}
		
		//所有物品排序(首先类型降序； 按照requiredLevel升序,然后按照requireInteligenceLevel升序,最后按照priceBones升序)
		public function sortObjects(a:int, b:int):int
		{
			var objectConfigA:ObjectConfig = Config.objectConfigs[a];
			var objectConfigB:ObjectConfig = Config.objectConfigs[b];
			//类型降序
			if (objectConfigA.type < objectConfigB.type)
			{
				return 1;
			}
			else if (objectConfigA.type > objectConfigB.type)
			{
				return -1;
			}
			else
			{	//需要等级升序	
				if (objectConfigA.requireLevel < objectConfigB.requireLevel)
				{
					return -1;
				}
				else if (objectConfigA.requireLevel > objectConfigB.requireLevel)
				{
					return 1;
				}
				else
				{	//需要智力升序
					if (objectConfigA.requireInteligenceLevel < objectConfigB.requireInteligenceLevel)
					{
						return -1;
					}
					else if (objectConfigA.requireInteligenceLevel > objectConfigB.requireInteligenceLevel)
					{
						return 1;
					}
					else
					{	
						//价格升序
						if (objectConfigA.priceBones < objectConfigB.priceBones)
						{
							return -1;
						}
						else if (objectConfigA.priceBones > objectConfigB.priceBones)
						{
							return 1;
						}
						else
						{
							return 0;
						}
					}
				}
			}
		}
		
		//按重量升序；然后按价格升序
		public function sortObjectsOnWeightAndPrice(a:int, b:int):int
		{
			var objectConfigA:ObjectConfig = Config.objectConfigs[a];
			var objectConfigB:ObjectConfig = Config.objectConfigs[b];
			if (objectConfigA.weight < objectConfigB.weight)
			{
				return -1;
			}
			else if (objectConfigA.weight > objectConfigB.weight)
			{
				return 1;
			}
			else
			{
				if (objectConfigA.priceBones < objectConfigB.priceBones)
				{
					return -1;
				}
				else if (objectConfigA.priceBones > objectConfigB.priceBones)
				{
					return 1;
				}
				else
				{
					return 0;
				}
			}
		}
		
		//按类型升序；按照等级升序；按照价格升序
		public function sortEquipsOnType(a:int, b:int):int
		{
			var equipConfigA:EquipConfig = Config.objectConfigs[a] as EquipConfig;
			var equipConfigB:EquipConfig = Config.objectConfigs[b] as EquipConfig;
			//按类型升序
			if (equipConfigA.equipType < equipConfigB.equipType)
			{
				return -1;
			}
			else if (equipConfigA.equipType > equipConfigB.equipType)
			{
				return 1;
			}
			//按照等级升序
			else
			{
				if (equipConfigA.requireLevel < equipConfigB.requireLevel)
				{
					return -1;
				}
				else if (equipConfigA.requireLevel > equipConfigB.requireLevel)
				{
					return 1;
				}
				//按照价格升序
				else
				{
					if (equipConfigA.priceBones < equipConfigB.priceBones)
					{
						return -1;
					}
					else if (equipConfigA.priceBones > equipConfigB.priceBones)
					{
						return 1;
					}
					else
					{
						return 0;
					}
				}
			}
		}

	}
}